package com.estronger.shopmanage.gaodemap;

import java.util.ArrayList;
import com.estronger.shopmanage.R;
import java.util.List;
import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.NaviLatLng;
import com.estronger.shopmanage.base.MyApplication;

public class GPSNavigation extends GaodeBaseActivity {
	List<NaviLatLng> startList = new ArrayList<NaviLatLng>();
	List<NaviLatLng> endList = new ArrayList<NaviLatLng>();
	private NaviLatLng startLatlng = null;

	public AMapLocationClientOption mLocationOption = null;
	public AMapLocationClient mLocationClient = null;
	public AMapLocationListener mLocationListener = new AMapLocationListener() {
		@Override
		public void onLocationChanged(AMapLocation amapLocation) {
			if (amapLocation != null) {
				if (amapLocation.getErrorCode() == 0) {
					MyApplication.latitude = amapLocation.getLatitude();
					MyApplication.longitude = amapLocation.getLongitude();
					MyApplication.address = amapLocation.getAddress();
					MyApplication.province = amapLocation.getProvince();
					startLatlng = new NaviLatLng(MyApplication.latitude,
							MyApplication.longitude);
					startList.add(startLatlng);
					mAMapNaviView.setAMapNaviViewListener(GPSNavigation.this);
					if (0 != MyApplication.latitude
							&& 0 != MyApplication.longitude) {
						if (mLocationClient != null)
							mLocationClient.stopLocation();
					}
				} else {
					Log.d("MM",
							"location Error, ErrCode:"
									+ amapLocation.getErrorCode()
									+ ", errInfo:"
									+ amapLocation.getErrorInfo());
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_navigation1);

		mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
		mAMapNaviView.onCreate(savedInstanceState);
		// 终点
		NaviLatLng endLatlng = new NaviLatLng(Double.valueOf(getParamValue(
				"dep_latitude").toString()), Double.valueOf(getParamValue(
				"dep_longitude").toString()));

		// 我的位置
		if (MyApplication.latitude == 0 || MyApplication.longitude == 0) {
			try {
				mLocationClient = new AMapLocationClient(getApplicationContext());
			} catch (Exception e) {
				e.printStackTrace();
			}
			// AMapLocationClientOption
			mLocationOption = new AMapLocationClientOption();
			// ���ö�λģʽΪAMapLocationMode.Battery_Saving
			mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
			// mLocationOption.setOnceLocation(true);
			// mLocationOption.setOnceLocationLatest(true);
			mLocationOption.setInterval(2000);
			mLocationOption.setNeedAddress(true);
			if (mLocationClient != null) {
				mLocationClient.setLocationOption(mLocationOption);
				mLocationClient.startLocation();
				mLocationClient.startLocation();
				mLocationClient.setLocationListener(mLocationListener);
			}
		} else {
			startLatlng = new NaviLatLng(MyApplication.latitude,
					MyApplication.longitude);
			mAMapNaviView.setAMapNaviViewListener(this);
		}

		startList.add(startLatlng);
		endList.add(endLatlng);

	}

	@Override
	public void onInitNaviSuccess() {
		super.onInitNaviSuccess();
		/**
		 * 方法: int strategy=mAMapNavi.strategyConvert(congestion,
		 * avoidhightspeed, cost, hightspeed, multipleroute); 参数:
		 * 
		 * @congestion 躲避拥堵
		 * @avoidhightspeed 不走高速
		 * @cost 避免收费
		 * @hightspeed 高速优先
		 * @multipleroute 多路径
		 * 
		 *                说明: 以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，
		 *                如果为true则此策略会算出多条路线。 注意: 不走高速与高速优先不能同时为true
		 *                高速优先与避免收费不能同时为true
		 */
		int strategy = 0;
		try {
			// 再次强调，最后一个参数为true时代表多路径，否则代表单路径
			strategy = mAMapNavi.strategyConvert(true, false, false, false,
					false);

		} catch (Exception e) {
			e.printStackTrace();
		}
		mAMapNavi.calculateDriveRoute(startList, endList, null, strategy);
	}

	@Override
	public void onCalculateRouteSuccess() {
		super.onCalculateRouteSuccess();
		mAMapNavi.startNavi(NaviType.GPS);
	}

	public Object getParamValue(String key) {
		Bundle data = this.getIntent().getExtras();
		if (data != null) {
			return data.get(key);
		} else {
			return null;
		}
	}

}
